package com.xckj.cycling.framework.net;

import androidx.annotation.NonNull;

import java.io.IOException;
import java.util.List;

import okhttp3.Cookie;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @Author: ChenYao
 * @Describe: 头部拦截器
 * @Date: 2023/2/26 19:06
 */
public class HeadIntercept implements Interceptor {
    @NonNull
    @Override
    public Response intercept(@NonNull Chain chain) throws IOException {
        Request oldRequest = chain.request();
        String sessionId = "";
        List<Cookie> cookies = RetrofitManager.getInstance().cookieStore.get(oldRequest.url().host());
        if (cookies != null && cookies.size() > 0) {
            sessionId = cookies.get(0).value();
        }
        // TODO: 2023/2/26 到时候看看head传啥
        Request newRequest = oldRequest.newBuilder()
                .addHeader("sessionID", sessionId)
                .build();
        return chain.proceed(newRequest);
    }
}
